// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <boost/random/uniform_int_distribution.hpp>
#include <string>

#include <pollux/plan/input_generator.h>

using namespace kumo::pollux::plan;

namespace kumo::pollux::exec::test {

class ApproxDistinctInputGenerator : public InputGenerator {
 public:
  std::vector<VectorPtr> generate(
      const std::vector<TypePtr>& types,
      VectorFuzzer& fuzzer,
      FuzzerGenerator& rng,
      memory::MemoryPool* pool) override {
    if (types.size() != 2) {
      return {};
    }

    // Make sure to use the same value of 'e' for all batches in a given Fuzzer
    // iteration.
    if (!e_.has_value()) {
      // Generate value in [0.0040625, 0.26] range.
      static constexpr double kMin = 0.0040625;
      static constexpr double kMax = 0.26;
      e_ = kMin + (kMax - kMin) * boost::random::uniform_01<double>()(rng);
    }

    const auto size = fuzzer.getOptions().vectorSize;

    POLLUX_CHECK(
        types.back()->is_double(),
        "Unexpected type: {}",
        types.back()->toString());
    return {
        fuzzer.fuzz(types[0]),
        BaseVector::create_constant(DOUBLE(), e_.value(), size, pool)};
  }

  void reset() override {
    e_.reset();
  }

 private:
  std::optional<double> e_;
};

} // namespace kumo::pollux::exec::test
